<template>
  <div>
    <!-- 公告管理 -->
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="公告标题">
          <el-input
            v-model="title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="load(1)">查询</el-button>
          <el-button type="info" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>

      <!--按钮-->
      <el-row>
        <el-button type="danger" size="medium" @click="deleteByIds"
          >- 批量删除</el-button
        >
        <el-button type="primary" size="medium" @click="insert()">+ 新增公告</el-button>
      </el-row>
    </el-card>

     <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="content" label="内容"> </el-table-column>
      <el-table-column prop="userid" label="管理员ID"> </el-table-column>
      <el-table-column align="center" label="发布时间">
        <template slot-scope="scope">
            {{scope.row.date ? scope.row.date.replace('T',' '):''}}
          </template>
      </el-table-column>
      <el-table-column label="是否公开" align="center"> 
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.open"
            @change="changeOpen(scope.row)"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>

        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="update(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            @click="deleteById(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

<el-row>
      <div class="block" style="margin:10px 0">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[100, 200, 300, 400]"
      :page-size="pageSize"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
    </el-row>

     <el-dialog title="公告管理" :visible.sync="formVisible"  width="40%">
        <el-form :model="form" label-width="80px" style="padding-right : 20px" :rules="rules">
            <el-form-item label="标题" prop="title">
            <el-input v-model="form.title"></el-input>
            </el-form-item>
            <el-form-item label="内容" prop="content">
            <!-- <el-input v-model="form.content"></el-input> -->
             <el-input type="textarea"
              autosize  maxlength="250"
              show-word-limit v-model="form.content">
              </el-input>

            </el-form-item>
            <el-form-item label="管理员ID" prop="id">
            <el-input v-model="form.userid" disabled></el-input>
            </el-form-item>

            <el-form-item>
            <el-button type="primary" @click="save()">提交</el-button>
            <el-button @click="formVisible = false">取消</el-button>
            </el-form-item>
      </el-form>
    </el-dialog>




  </div>
</template>

<script>
export default {
    name: 'ANotice',
    data () {
      return {
        user: JSON.parse(localStorage.getItem("user") || "{}"),
        title:'',
        tableData:[], // 所有数据
        page : 1, // 当前的页码
        pageSize : 10, // 每页显示的个数
        total : 0,
        ids : [],
        formVisible : false,
        form : {},
        rules: {
          
          title: [
            { required: true, message: '请输入标题', trigger: 'blur' },
            { min: 1, max: 150, message: '长度在 1 到 150 个字符', trigger: 'blur' }
          
          ]

      },

      }
    },
    created () {
      this.load(1);
    },
    methods: {
      changeOpen(form){
          this.form = JSON.parse(JSON.stringify(form));
          this.save();

      },
      insert(){
        this.form = {};
        this.form.userid = this.user.id;
        this.formVisible = true;
      },
      update(row){ // 编辑
        this.form = JSON.parse(JSON.stringify(row)); // 给form对象赋值
        this.formVisible = true; // 显示弹窗
      },
      save(){ 
          this.$request({
              url : '/notices',
              method : this.form.id ? 'put' : 'post',
              data : this.form
          }).then(res => {
              if(res.code === 1){
                  this.$message({
                  //message: '登陆成功',
                  message:res.msg,
                  type: 'success'
              });
              this.load(1);
              this.formVisible = false;
              }else{
                  this.$message({
                  //message: '登陆失败',
                  message: res.msg,
                  type: 'warning'
              });
              }
          });
      },
      handleCurrentChange(page){
      //  this.page = page;
        this.load(page);
      }, 
      handleSelectionChange(rows) {
          this.ids = rows.map(v => v.id);
      },
      load(page){ // 分页查询
        if (page) {
            this.page = page;
        }
        this.$request.get('/notices',{
            params : {
                page : this.page,
                pageSize : this.pageSize,
                title: this.title,

            }
        }).then((res) => {
            this.total = res.data.total;
            this.tableData = res.data.rows;
        }).catch((err) => {
            console.log(err);
        });
    },
    reset(){
        this.title = '';
        this.load(1);
    },
     deleteById(id){
       this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/notices/' + id ).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
    deleteByIds(){
      if(!this.ids.length){
        this.$message.warning('请选择数据！');
        return;
      }

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((result) => {
        console.log(result);
        this.$request.delete('/notices/' + this.ids).then((res) => {
          if (res.code === 1) {
            this.$message({
            //message: '登陆成功',
            message:res.msg,
            type: 'success'
          });
          this.load(1);
          } else {
            this.$message({
            //message: '登陆失败',
            message: res.msg,
            type: 'warning'
          });
          }
        }).catch((error) => {
          console.log(error);
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      }).catch((err) => {
        console.log(err);
      });
    },
      
    }

}
</script>

<style>

</style>